package com.talent.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.talent.web.annotation.TalentLog;
import com.talent.web.config.security.TalentUserDetails;
import com.talent.web.enums.ActionType;
import com.talent.web.model.TalentResult;
import com.talent.web.model.dto.SysUserDto;
import com.talent.web.model.query.UserQuery;
import com.talent.web.model.vo.OptionVo;
import com.talent.web.model.vo.SysUserVo;
import com.talent.web.model.vo.WsUserInfo;
import com.talent.web.service.ISysUserService;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
/**
 * @ClassName：UserController
 * @Author: hszhou
 * @Date: 2024/10/21 13:35
 * @Description: 用户龙之期
 */
@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private ISysUserService userService;



    @PostMapping
    @TalentLog(module="用户",action = ActionType.INSERT)
    public TalentResult<Void> addSysUser(@RequestBody SysUserDto dto){
        userService.addSysUser(dto);
        return TalentResult.success();
    }

    @DeleteMapping("{id}")
    @TalentLog(module="用户",action = ActionType.DELETE)
    public TalentResult<Null> deleteById(@PathVariable("id")String id){
        userService.deleteById(id);
        return TalentResult.success(null);
    }

    @PutMapping
    @TalentLog(module="用户",action = ActionType.UPDATE)
    public TalentResult<Null> updateUser(@RequestBody SysUserDto dto) throws Exception{
        userService.updateUser(dto);
        return TalentResult.success(null);
    }

    @GetMapping("page")
    public TalentResult<IPage<SysUserVo>> page(@RequestParam(defaultValue = "1") int current,
                                               @RequestParam(defaultValue = "10") int pageSize, UserQuery query){
        IPage<SysUserVo> page = userService.page(current, pageSize, query);
        return TalentResult.success(page);
    }
    @GetMapping("/{id}")
    public TalentResult<SysUserVo> detail(@PathVariable("id")Long id){
        return TalentResult.success(userService.detail(id));
    }

    @GetMapping("/user-info")
    public TalentResult<WsUserInfo> userInfo(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        TalentUserDetails  userDetails =(TalentUserDetails) authentication.getPrincipal();
        WsUserInfo wsUserInfo = new WsUserInfo();
        wsUserInfo.setName(userDetails.getUsername());
        List<String> authority = userService.getPermission(userDetails.getId());
        wsUserInfo.setAuthority(authority);
        wsUserInfo.setAvatar(null);
        wsUserInfo.setId(userDetails.getId());
        wsUserInfo.setCount(10);
        return TalentResult.success(wsUserInfo);
    }

    @GetMapping("option")
    public TalentResult<List<OptionVo>> option(){
        List<OptionVo> rs = new ArrayList<>();
        OptionVo v1 = new OptionVo("1","男");
        OptionVo v2= new OptionVo("1","女");
        rs.add(v1);rs.add(v2);
        return TalentResult.success(rs);
    }

}
